package com.lemon.smartsafetynew.ui.fragment.electrical;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshAddTitleFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.electrical.ElectricalCompanyVo;
import com.lemon.smartsafetynew.entity.electrical.ElectricalItemVo;
import com.lemon.smartsafetynew.net.services.ElectricalServices;
import com.lemon.smartsafetynew.ui.activity.electrical.ElectricalSafeSensorListActivity;
import com.lemon.smartsafetynew.ui.adapter.electrical.ElectricalItemAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * 实时监测 -- 电气火灾监测的列表
 * Created by Andrew on 2018/3/12.
 */

public class ElectricalListFragment extends BasePullToRefreshAddTitleFragment<ElectricalItemVo, List<ElectricalItemVo>>
        implements RecyclerTClickListener<ElectricalItemVo> {

    private static ElectricalCompanyVo companyVo = null;

    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        if (!UserTypeUtil.isGovType(AccountUtil.getUserType())) { //非政府人员使用模块
            params.put("companyId", AccountUtil.getCompanyId());
        } else {
            if (companyVo != null) {
                params.put("companyId", companyVo.getCompanyId());
            }
        }
        params.put("boxId", companyVo.getBoxId());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        ElectricalServices.init().getMonitorPointList(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {
        LemonRequestParams params = new LemonRequestParams("");
        if (!UserTypeUtil.isGovType(AccountUtil.getUserType())) { //非政府人员使用模块
            params.put("companyId", AccountUtil.getCompanyId());
        } else {
            if (companyVo != null) {
                params.put("companyId", companyVo.getCompanyId());
            }
        }
        params.put("boxId", companyVo.getBoxId());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        ElectricalServices.init().getMonitorPointList(RefreshType.LOAD_MORE, params);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<ElectricalItemVo> getLemonAdapter() {
        return new ElectricalItemAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_ELECTRICAL_POINTS_LIST;
    }

    @Override
    public void onDataList(List<ElectricalItemVo> data) {

    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_ELECTRICAL_COMPANY_INFO)) {
                companyVo = (ElectricalCompanyVo) bundle.getSerializable(KeyConstants.KEY_ELECTRICAL_COMPANY_INFO);
            }
        }
    }

    public static ElectricalListFragment newInstance(ElectricalCompanyVo data) {
        Bundle args = new Bundle();
        if (data != null) {
            args.putSerializable(KeyConstants.KEY_ELECTRICAL_COMPANY_INFO, data);
        }

        ElectricalListFragment fragment = new ElectricalListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        llTilte.setVisibility(View.VISIBLE);
        if (companyVo != null) {
            tvTitle.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(companyVo.getCompanyName()));
        } else {
            tvTitle.setText("");
        }
    }

    @Override
    public void onItemClick(ElectricalItemVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_ELECTRICAL_ITEM, data);
        UIHelper.showActivity(getmContext(), bundle, ElectricalSafeSensorListActivity.class);
    }
}
